<?php

namespace Home\Controller;
use Think\Controller; 
class InstallController extends Controller {
	
	public function __construct(){
		parent::__construct();
		
		if(empty($_SESSION['user']))
		{
			$this->error('对不起,您还没有登录!请先登录!', U('Login/login'), 1);
			
		}
	}
	
	
	//添加账号 没有就新增
	public function add_account(){
		if($_POST){
			$data['username']		=		empty($_REQUEST['user_name'])?'':trim($_REQUEST['user_name']);
			$data['password']		=		empty($_REQUEST['password'])?'':md5(trim($_REQUEST['password']));
			$data['cost_price']		=		empty($_REQUEST['cost_price'])?'':$_REQUEST['cost_price'];
			$data['supplier']		=		empty($_REQUEST['supplier'])?'':$_REQUEST['supplier'];
			$data['orders']			=		empty($_REQUEST['check_order'])?'':$_REQUEST['check_order'];
			$data['purchase']		=		empty($_REQUEST['purchase'])?'':$_REQUEST['purchase'];
			$data['to_purchase']	=		empty($_REQUEST['to_purchase_del'])?'':$_REQUEST['to_purchase_del'];
			$data['delivery']		=		empty($_REQUEST['delivery_del'])?'':$_REQUEST['delivery_del'];
			$data['complete']		=		empty($_REQUEST['complete_del'])?'':$_REQUEST['complete_del'];
			$data['order']			=		empty($_REQUEST['order'])?'':$_REQUEST['order'];
			$data['data']			=		empty($_REQUEST['data'])?'':$_REQUEST['data'];
			$data['system_install']	=		empty($_REQUEST['system_install'])?'':trim($_REQUEST['system_install']);
			$data['new_finish']		=		empty($_REQUEST['new_finish'])?'2':trim($_REQUEST['new_finish']);

			$data['look_details']	=		empty($_REQUEST['look_details'])?'2':trim($_REQUEST['look_details']);
			$data['del_history']	= 		empty($_REQUEST['del_history'])?0:trim($_REQUEST['del_history']);
			$data['premarks']		= 		empty($_REQUEST['remarks'])?0:trim($_REQUEST['remarks']);
			$data['select']		= 		empty($_REQUEST['select'])?0:trim($_REQUEST['select']);
			$account = M("account");
			
			
			$where['username'] = $data['username'];
			$result = $account->where($where)->find();
			if($result == false){
				$res = $account->add($data);
				$this->success("添加成功","account_list",'3');exit;
			}else{
				$this->error("用户已存在",'add_account','3');exit;
			}
		}
		$this->display();	
	}
	
	//分页显示账号列表
	public function account_list(){
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$username=empty($_REQUEST['username'])?'':$_REQUEST['username'];
		
		
		
		$account =D('account'); 
		$res = $account->account_list($pageNo,$pageSize,$where);
		$result = $res['account_list'];
		$count = $res['count'];
		
		if($_POST && !empty($username)){
			$name = array('like','%'.$username.'%');
			$result = M('account')->field('id,username')->where(array('username'=>$name))->select();
			$count['count'] = count($result);
			
			
		}
		$this->assign("account_info",$result);
		$this->assign("count",$count);
		$this->display();	
	}
	
	//账号权限管理
	public function edit_account_settings(){
		$where['id']=$_REQUEST['id'];
		$account = M("account");
		$account = $account->where($where)->find();
		
		$cost 			= 	$account['cost_price'];
		$supplier 		= 	$account['supplier'];
		$orders 		= 	$account['orders'];
		$purchase 		= 	$account['purchase'];
		$to_purchase	= 	$account['to_purchase'];
		$delivery		=	$account['delivery'];
		$complete 		= 	$account['complete'];
		$order			=	$account['order'];
		$data			=	$account['data'];
		$system_install = 	$account['system_install'];
		$new_finish		=	$account['new_finish'];
		$look_details	=	$account['look_details'];
		$del_circulation	=	$account['del_circulation'];


		$this->assign('del_history',$account['del_history']);
		$this->assign('premarks',$account['premarks']);
		$this->assign('select',$account['select']);
		$this->assign("account",$account);
		$this->assign("supplier",$supplier);
		$this->assign("orders",$orders);
		$this->assign("purchase",$purchase);
		$this->assign("to_purchase",$to_purchase);
		$this->assign("delivery",$delivery);
		$this->assign("complete",$complete);
		$this->assign("new_finish",$new_finish);
		$this->assign("look_details",$look_details);
		$this->assign("order",$order);
		$this->assign("data",$data);
		$this->assign("system_install",$system_install);
		$this->assign("cost",$cost);
		$this->assign("del_circulation",$del_circulation);
		$this->display();	
	}
	
	//账号权限修改
	public function edit_account(){
		$where['id']				=		$_POST['id'];
		$data['username']			=		trim($_POST['username']);
		$password			=		trim($_POST['password']);
		$password = trim($_POST['password']);
		$md5 = M('account')->field('password')->where(array('username'=>$data['username']))->find();
		if($md5['password'] != $password){
			$data['password']			=		md5(trim($_POST['password']));
		}else{
			$data['password'] = $md5['password'];
		}
		$data['cost_price']			=		$_POST['cost_price'];
		$data['supplier']			=		$_POST['supplier'];
		$data['orders']				=		$_POST['orders'];
		$data['purchase']			=		$_POST['purchase'];
		$data['to_purchase']		=		$_POST['to_purchase_del'];
		$data['delivery']			=		$_POST['delivery_del'];
		$data['complete']			=		$_POST['complete_del'];
		$data['order']				=		$_POST['order'];
		$data['data']				=		$_POST['data'];
		$data['system_install']		=		$_POST['system_install'];
		$data['new_finish']			=		$_POST['new_finish'];
		$data['look_details']		=		$_POST['look_details'];
		$data['del_history']		= 		empty($_REQUEST['del_history'])?0:trim($_REQUEST['del_history']);
		$data['premarks']			= 		empty($_REQUEST['remarks'])?0:trim($_REQUEST['remarks']);
		$data['select']				= 		empty($_REQUEST['select'])?0:trim($_REQUEST['select']);
		$data['del_circulation']			=		$_POST['del_circulation'];
		$data['del_offshelf_infos']			=		$_POST['del_offshelf_infos'];
		if(empty($data['cost_price'])){
			$data['cost_price']='2';	
		}
		if(empty($data['supplier'])){
			$data['supplier']='2';	
		}
		if(empty($data['order'])){
			$data['order']='2';	
		}
		if(empty($data['data'])){
			$data['data']='2';	
		}
		if(empty($data['to_purchase'])){
			$data['to_purchase']='2';	
		}
		if(empty($data['delivery'])){
			$data['delivery']='2';	
		}
		if(empty($data['complete'])){
			$data['complete']='2';	
		}
		if(empty($data['new_finish'])){
			$data['new_finish']='2';	
		}
		if(empty($data['look_details'])){
			$data['look_details']='2';	
		}
		if(empty($data['del_circulation'])){
			$data['del_circulation']='0';	
		}
		if(empty($data['del_offshelf_infos'])){
			$data['del_circulation']='0';	
		}

		//dump($data);exit;
		$account = M("account");
		$result = $account->where($where)->save($data);
		
		if($result){
			$this->success("修改成功","account_list","1");
		}else{
			$this->error("您没有权限修改配置","account_list","1");
		}
		
		
	}
	
	//删除账号
	public function del_account(){
		$where['id']=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		
		$account = M("account");
		$result = $account->where($where)->delete();
		if($result){
			
			header("Location:".$_SERVER['HTTP_REFERER']);
		}
	}
	
	//供应商列表
	public function supplier_list(){
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$full_name=empty($_REQUEST['full_name'])?'':$_REQUEST['full_name'];

		$for_short=empty($_REQUEST['for_short'])?'':$_REQUEST['for_short'];
		if(!empty($full_name) ||!empty($for_short)){
			$full_name=trim($full_name);
			$where['full_name'] = array('like',"%$full_name%");
			$where['for_short'] = array('like',"%$full_name%");
			$where['_logic'] = 'OR';
		}
		
		$supplier =D('supplier'); 
		$res = $supplier->supplier_list($pageNo,$pageSize,$where);
		$result = $res['supplier_list'];
		$count = $res['count'];
		$this->assign('full_name',$full_name);
		$this->assign('pageNo',$pageNo);
		$this->assign("pageSize",$pageSize);
		$this->assign("supplier_list",$result);
		$this->assign("count",$count);
		$this->display();	
	}
	
	//供应商添加
	public function supplier_add(){
		$supplier = M("supplier");
		$where['full_name'] = $_REQUEST['full_name'];
		$supplier_info = $supplier->where($where)->find();
		if($supplier_info){
			$this->error("添加失败,该供应商已存在","supplier_list","3");
		}else{
			if($_POST){
				$data['full_name']		=		empty($_REQUEST['full_name'])?'':$_REQUEST['full_name'];
				$data['for_short']		=		empty($_REQUEST['for_short'])?'':$_REQUEST['for_short'];
				$data['principal']		=		empty($_REQUEST['principal'])?'':$_REQUEST['principal'];
				$data['contact']		=		empty($_REQUEST['contact'])?'':$_REQUEST['contact'];
				$data['address']		=		empty($_REQUEST['address'])?'':$_REQUEST['address'];
				$data['account']		=		empty($_REQUEST['account'])?'':$_REQUEST['account'];
				$data['buyer']			=		empty($_REQUEST['buyer'])?'':$_REQUEST['buyer'];
			
				$supplier_add = $supplier->add($data);
			}
		}
		
		$this->success("添加成功","supplier_list",'3');exit;
	}
	
	//供应商列表修改
	public function supplier_edit(){
		$supplier = M("supplier");
		$id 	= 	$_REQUEST['id'];
		$name=empty($_REQUEST['name'])?'':$_REQUEST['name'];
		$value=empty($_REQUEST['value'])?'':$_REQUEST['value'];
		$data[$name]=$value;
		$yname = $supplier->field('for_short')->where("id='$id'")->find();
		$yname = $yname['for_short'];
		$res = $supplier->where("id='$id'")->save($data);
		if($res!==false)
		{   
			$where = " supplier ='$yname'";
			M("product")->where($where)->save($data);
			$msg='success';
		}
		else
		{
			$msg='fail';
		}
		
		echo $msg;die;
	}
	
	//供应商删除
	public function supplier_delete(){
	   $where['id']=empty($_REQUEST['id'])?0:$_REQUEST['id'];
	   $supplier = M("supplier");
	   $res=$supplier->where($where)->delete();
	   header('Location:'.$_SERVER['HTTP_REFERER']);   
	}
	
	//价格公式管理
	public function price_control(){
		/*$control_list = M('price_control')->field('id,type')->select();
			foreach($control_list as $key=>$val){
				$res1 = M('product')->where(array('product_type'=>$val['type']))->select();
			}*/
		
		$result = M("product")->field('price_type')->group('price_type')->order('id')->select();
	    foreach($result as $k=>$v){
	        if (!$v['price_type']) {
	            continue;
            }
			$res = M('price_control')->where(array('type'=>$v['price_type']))->find();
			if($res==NULL){
				$add =  M('price_control')->add(array('type'=>$v['price_type']));
			}
		}

		$price_control = M("price_control");
		//dump(I('post.'));exit;
		if(I("post.edit",'','htmlspecialchars')){
			$where = I("post.edit",'','htmlspecialchars');


			//批发散货价格
			if($where == 'ali_lt'){
				//echo "This is ali_lt";exit;
				$ali_lt = I('post.ali_lt','','htmlspecialchars');
				$id=I('post.id','','htmlspecialchars');
				$type = I('post.type','','htmlspecialchars');
				$product_list = M("product")->where(array('price_type'=>$type))->select();
				//dump($product_list);exit;
				
				foreach($product_list as $key=>$val){
					$price = $val['cost_price'];
					$x = $price;
					$str1 = str_replace('x','$x',$ali_lt);
					$str2 = str_replace('y1','$y1',$str1);
					$data['ali_lt']=eval("return $str2;"); //$y1 是经过公式 和 $val['cost_price'] 计算得出; 
					/*echo $ali_lt."<br>";
					echo "x=".$x."<br>";
					echo "str2=".$str2."<br>";	
					echo $data['ali_lt'];exit; */
					$res = M('product')->where(array('id'=>$val['id']))->save(array('ali_lt'=>$data['ali_lt']));
				}
				
				$res = $price_control->where(array('id'=>$id))->save(array('ali_lt'=>$ali_lt));
				
				header('Location:'.$_SERVER['HTTP_REFERER']);   

				//批发订货价
			}else if($where == 'ali_gt'){
				$ali_gt = I('post.ali_gt','','htmlspecialchars');
				$id=I('post.id','','htmlspecialchars');
				$type = I('post.type','','htmlspecialchars');
				$product_list = M("product")->where(array('price_type'=>$type))->select();
				foreach($product_list as $key=>$val){
					$price = $val['cost_price'];
					$x = $price;
					$str1 = str_replace('x','$x',$ali_gt);
					$str2 = str_replace('y2','$y2',$str1);
					$data['ali_gt']=eval("return $str2;");

					$res = M('product')->where(array('id'=>$val['id']))->save(array('ali_gt'=>$data['ali_gt']));
				}
				
				$res = $price_control->where(array('id'=>$id))->save(array('ali_gt'=>$ali_gt));
				header('Location:'.$_SERVER['HTTP_REFERER']);  

				//零售价
			}else if($where == 'aliexpress'){
				$aliexpress = I('post.aliexpress','','htmlspecialchars');
				
				$id=I('post.id','','htmlspecialchars');
				$type = I('post.type','','htmlspecialchars');
				$product_list = M("product")->where(array('price_type'=>$type))->select();
				
				foreach($product_list as $key=>$val){
					$price = $val['cost_price'];
					$x = $price;
					$str1 = str_replace('x','$x',$aliexpress);
					$str2 = str_replace('y3','$y3',$str1);
					$data['aliexpress']=eval("return $str2;");
					$res = M('product')->where(array('id'=>$val['id']))->save(array('aliexpress'=>$data['aliexpress']));
				}
				
				$res = $price_control->where(array('id'=>$id))->save(array('aliexpress'=>$aliexpress));
				header('Location:'.$_SERVER['HTTP_REFERER']);  
			}
//else{
//				$ioffer = I('post.ioffer','','htmlspecialchars');
//
//				$id=I('post.id','','htmlspecialchars');
//				$type = I('post.type','','htmlspecialchars');
//				$product_list = M("product")->where(array('product_type'=>$type))->select();
//
//				foreach($product_list as $key=>$val){
//					$price = $val['cost_price'];
//					$x = $price;
//					$str1 = str_replace('x','$x',$ioffer);
//					$str2 = str_replace('y4','$y4',$str1);
//					$data['ioffer']=eval("return $str2;");
//					$res = M('product')->where(array('id'=>$val['id']))->save(array('ioffer'=>$data['ioffer']));
//				}
//
//				$res = $price_control->where(array('id'=>$id))->save(array('ioffer'=>$ioffer));
//				header('Location:'.$_SERVER['HTTP_REFERER']);
//			}
		}
		/*
		$price_control = M("price_control");
		if($_POST){
		$data['ali_lt']=empty($_REQUEST['ali_lt'])?'':$_REQUEST['ali_lt'];
		$data['ali_gt']=empty($_REQUEST['ali_gt'])?'':$_REQUEST['ali_gt'];
		$data['aliexpress']=empty($_REQUEST['aliexpress'])?'':$_REQUEST['aliexpress'];
		$data['ioffer']=empty($_REQUEST['ioffer'])?'':$_REQUEST['ioffer'];
		
		
		$res = $price_control->find();
			if($res){
				$where['id'] = "1";
				$result = $price_control->where($where)->save($data);
				$this->success("修改成功","price_control",'3');exit;
			}else{
				$result = $price_control->add($data);
				$this->success("添加成功","price_control",'3');ex t;
			}
		}
		*/

		foreach($result as $k=>$v){
			if($k=='0'){
				$where	= " type = '".$v['price_type']."'" ;
			}else{
				$where .= " or type = '".$v['price_type']."'" ;
			}
		}
		$control = $price_control->where($where)->select();
		//dump($where);exit;
		$this->assign("result",$control);
		$this->display();	
	}

	
	//团队列表
	public function salesman_team(){
		$team = M("team");
		$team_list = $team->group('team_name')->select();
		foreach($team_list as $key=>$val){
			$team = M("team")->where(array('team_name'=>$val['team_name']))->select();
			$arr = explode("|",$val['team_members']);
			$team_list[$key]['team_members'] = implode('<br/>',$arr);
		}
		$this->assign("team_list",$team_list);
		$this->display();	
	}
	
	//添加团队
	public function add_team(){
		if($_POST){
			$data['team_name']			=	empty($_REQUEST['team_name'])?'':trim($_REQUEST['team_name']);
			$data['team_leadership']	=	empty($_REQUEST['team_leadership'])?'':trim($_REQUEST['team_leadership']);
			$data['team_members']		= 	empty($_REQUEST['team_members'])?'':trim($_REQUEST['team_members']);
			$team = M("team");
			$where['team_name'] = $data['team_name'];
			$result = $team->where($where)->find();
			
			if($result){
				$this->error("团队已存在，请另起团队名称","add_team",'3');exit;
			}else{
				$arr_list = explode("\n",trim($data['team_members'],"\n"));
				foreach($arr_list as $val){
					$res = $team->add(array('team_name'=>$data['team_name'],'team_leadership'=>$data['team_leadership'],'team_members'=>$data['team_members']));
				}
				//$res = $team->add($data);
				if($res){
					$this->success("添加成功","salesman_team",'3');exit;
				}else{
					$this->error("添加失败","add_team",'3');exit;
				}
			}
		}
		$this->display();	
	}
	
	//修改团队信息
	public function team_edit(){
		$where['id']=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$team = M("team");
		$team_info = $team->where($where)->find();
		
		if($_POST){
			$data['team_name']			=	empty($_REQUEST['team_name'])?'':trim($_REQUEST['team_name']);
			$data['team_leadership']	=	empty($_REQUEST['team_leadership'])?'':trim($_REQUEST['team_leadership']);
			$data['team_members']		= 	empty($_REQUEST['team_members'])?'':trim($_REQUEST['team_members']);
		
			$result = $team->where($where)->save($data);
			$this->error("修改成功");exit;
			
		}
		$this->assign("team_info",$team_info);
		$this->display();
	}
	
	//删除团队
	public function team_dele(){
		$where['id']=empty($_REQUEST['id'])?'':$_REQUEST['id'];
		$team = M("team");
		$team->where($where)->delete();	
		$this->success("删除成功","salesman_team",'3');exit;
	}
	
	//自定义管理
	public function custom(){
		$where['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
		$custom = M("custom");
		$result = $custom->find();

		$where2['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
		$platform = M("platform");
		$result2 = $platform->find();

		if($_POST){
			
			$data['one']			=	empty($_REQUEST['one'])?'':trim($_REQUEST['one']);
			$data['two']			=	empty($_REQUEST['two'])?'':trim($_REQUEST['two']);
			$data['three']			=	empty($_REQUEST['three'])?'':trim($_REQUEST['three']);
			$data['four']			=	empty($_REQUEST['four'])?'':trim($_REQUEST['four']);
			
			if($result){
				$res = $custom->where($where)->save($data);
			}else{
				$res = $custom->add($data);
			}
		}
		$this->assign("result",$result);
		$this->assign("result2",$result2);
		$this->display();
	}


	//自定义管理-> 平台设定
	public function platform_set(){
		$where['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
		$platform = M("platform");
		$result = $platform->find();
		if($_POST){
			
			$data['platform1']			=	empty($_REQUEST['platform1'])?'':trim($_REQUEST['platform1']);
			$data['platform2']			=	empty($_REQUEST['platform2'])?'':trim($_REQUEST['platform2']);
			$data['platform3']			=	empty($_REQUEST['platform3'])?'':trim($_REQUEST['platform3']);
			$data['platform4']			=	empty($_REQUEST['platform4'])?'':trim($_REQUEST['platform4']);
			$data['platform5']			=	empty($_REQUEST['platform5'])?'':trim($_REQUEST['platform5']);
			$data['platform6']			=	empty($_REQUEST['platform6'])?'':trim($_REQUEST['platform6']);
			$data['platform7']			=	empty($_REQUEST['platform7'])?'':trim($_REQUEST['platform7']);
			$data['platform8']			=	empty($_REQUEST['platform8'])?'':trim($_REQUEST['platform8']);
			
			if($result){ //有记录就改， 没记录就添加
				$res = $platform->where($where)->save($data);
				//dump($data);exit;
				if($res){
					$this->success('修改成功');
				}else{$this->error('修改失败');}
			}else{
				$res = $platform->add($data);
				if($res){
					$this->success('添加成功');
				}else{$this->error('添加失败');}
			}
		}
		
	}
	
	//汇率设置
	public function exchange_rate(){
		$where['id']			=	empty($_REQUEST['id'])?1:trim($_REQUEST['id']);
		$result = M("exchange_rate")->where($where)->find();
		if($_POST){
			
			$data['USD']			=	empty($_REQUEST['USD'])?'':trim($_REQUEST['USD']);
			$data['GBP']			=	empty($_REQUEST['GBP'])?'':trim($_REQUEST['GBP']);
			$data['JPY']			=	empty($_REQUEST['JPY'])?'':trim($_REQUEST['JPY']);
			$data['EUR']			=	empty($_REQUEST['EUR'])?'':trim($_REQUEST['EUR']);
			$data['AUD']			=	empty($_REQUEST['AUD'])?'':trim($_REQUEST['AUD']);
			$data['CAD']			=	empty($_REQUEST['CAD'])?'':trim($_REQUEST['CAD']);
			$data['RUB']			=	empty($_REQUEST['RUB'])?'':trim($_REQUEST['RUB']);
			$data['MYR']			=	empty($_REQUEST['MYR'])?'':trim($_REQUEST['MYR']);
			$data['SGD']			=	empty($_REQUEST['SGD'])?'':trim($_REQUEST['SGD']);
			$data['PHP']			=	empty($_REQUEST['PHP'])?'':trim($_REQUEST['PHP']);
			$data['THB']			=	empty($_REQUEST['THB'])?'':trim($_REQUEST['THB']);
			$data['IDR']			=	empty($_REQUEST['IDR'])?'':trim($_REQUEST['IDR']);
			$data['MXN']			=	empty($_REQUEST['MXN'])?'':trim($_REQUEST['MXN']);
			
			if($result){
				$res = M("exchange_rate")->where($where)->save($data);
				$this->success("修改成功","exchange_rate",'3');exit;
			}else{
				$res = M("exchange_rate")->add($data);
				$this->success("添加成功","exchange_rate",'3');exit;
			}
		}
		$this->assign("result",$result);
		$this->display();
	}
	
	
	//收款方式设置
	public function payment(){
		if(I("post.payment_method","","htmlspecialchars")){
			$payment_method = I('post.payment_method','','htmlspecialchars');
			$verify = M("payment")->where(array('payment_method'=>$payment_method))->find();
			if(empty($verify)){
				$payment_list = M("payment")->add(array('payment_method'=>$payment_method));
				$this->success("添加成功","payment",'3');exit;
			}else{
				$this->success("添加失败，已存在","payment",'3');exit;
			}
		}
		
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		
		$install =D('Install'); 
		$res = $install->payment_list($pageNo,$pageSize);
		
		$payment_list = $res['payment_list'];
		$count = $res['count'];
		
		$this->assign("count",$count);
		$this->assign('payment_list',$payment_list);
		$this->display();
	}
	
	//删除支付方式
	public function payment_dele(){
		$id = I('get.p_id','','htmlspecialchars');
		$payment_list = M("payment")->where(array('id'=>$id))->delete();
		//$this->success("删除成功","payment",'3');exit();
		header('Location:'.$_SERVER['HTTP_REFERER']);  
	}
	
	
	//导出供应商列表
	public function daochu_excel(){
		$ids = I('get.tids','','htmlspecialchars');
		$id_list = explode(',',trim($ids,','));
		foreach($id_list as $key=>$val){
			$data[$key] = M('supplier')->field('for_short,full_name,principal,contact,address,account,buyer')->where(array('id'=>$val))->find();
			
		}
		
		error_reporting(E_ALL);
        date_default_timezone_set('Europe/London');
		  
		import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");
		import("Org.Util.PHPExcel.Style.NumberFormat");
		import("Org.Util.PHPExcel.Style.Alignment");
		import("Org.Util.PHPExcel.Style.Color");
		
		import("Org.Util.PHPExcel.Style.Border");
        $objPHPExcel = new \PHPExcel();
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
    
        $objActSheet = $objPHPExcel->getActiveSheet();
		$objActSheet->setCellValue('A1', '供应商简称');    
		$objActSheet ->setCellValue('B1', '供应商全称');
		$objActSheet ->setCellValue('C1', '负责人');
		$objActSheet->setCellValue('D1', '联系方式');    
		$objActSheet ->setCellValue('E1', '地址');
		$objActSheet ->setCellValue('F1', '收款账户');	
		$objActSheet->setCellValue('G1', "对接采购员");    
							  
		 /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
		foreach($data as $k => $v){
			 $k +=2;
			 $objPHPExcel->setActiveSheetIndex(0);
						 //Excel的第A列，uid是你查出数组的键值，下面以此类推
			 $objActSheet->setCellValue('A'.$k, $v['for_short']);    
			 $objActSheet ->setCellValue('B'.$k, $v['full_name']);
			 $objActSheet ->setCellValue('C'.$k, $v['principal']);
			 
			 $objActSheet->setCellValue('D'.$k, $v['contact']);    
			 $objActSheet ->setCellValue('E'.$k, $v['address']);
			 $objActSheet ->setCellValue('F'.$k, $v['account']);
			 
			 
			 $objActSheet->setCellValue('G'.$k, $v['buyer']);    
		}
		$filename = "供应商列表.xls";
		$date = date("Y-m-d",time());
		$filename .= "_{$date}.xls";
	
		//重命名表
		// $objPHPExcel->getActiveSheet()->setTitle('test');
		//设置活动单指数到第一个表,所以Excel打开这是第一个表
		$objPHPExcel->setActiveSheetIndex(0);
		header('Content-Type: application/vnd.ms-excel');
		header("Content-Disposition: attachment;filename=\"$filename\"");	//图片另存为
		header('Cache-Control: max-age=0');

		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		$objWriter->save('php://output'); //文件通过浏览器下载
		exit;
	}	
	
	
	
	
	
	/*public function exportexcel($data=array(),$title=array(),$filename='report'){
		header("Content-type:application/octet-stream");
		header("Accept-Ranges:bytes");
		header("Content-type:applicationnd.ms-excel");  
		header("Content-Disposition:attachment;filename=".$filename.".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		//导出xls 开始
		if (!empty($title)){
			foreach ($title as $k => $v) {
				//$title[$k]=iconv("UTF-8", "GB2312",$v);
				$title[$k]=$v;
			}
			$title= implode("\t", $title);
			echo "$title\n";
		}
		if (!empty($data)){
			foreach($data as $key=>$val){
				foreach ($val as $ck => $cv) {
					//$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
					$data[$key][$ck]= $cv;
				}
				$data[$key]=implode("\t", $data[$key]);
				
			}
			echo implode("\n",$data);
		}
		
	}*/
	
	//导入供应商
	public function impUser(){
	    $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('xls', 'xlsx');// 设置附件上传类
        $upload->rootPath  =     './Public/excel/'; // 设置附件上传根目录
        // 上传文件
        $info   =   $upload->uploadOne($_FILES['excel']);
        $filename = './Public/excel/'.$info['savepath'].$info['savename'];
        $exts = $info['ext'];
     
	 
        if(!$info)
		 {// 上传错误提示错误信息
              $this->error($upload->getError());
         }
		 else
		 {
			vendor('PHPExcel180.PHPExcel');
			//创建PHPExcel对象，注意，不能少了\
			$PHPExcel=new \PHPExcel();
			//如果excel文件后缀名为.xls，导入这个类
			if($exts == 'xls'){
				import("Org.Util.PHPExcel.Reader.Excel5");
				$PHPReader=new \PHPExcel_Reader_Excel5();
			}else if($exts == 'xlsx'){
				import("Org.Util.PHPExcel.Reader.Excel2007");
				$PHPReader=new \PHPExcel_Reader_Excel2007();
			}
			
			$data=array();
			$data_sum=array();
			
			//载入文件
			$PHPExcel=$PHPReader->load($filename);
			//获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
			$currentSheet=$PHPExcel->getSheet(0);
			//获取总列数
			$allColumn=$currentSheet->getHighestColumn();
			
			//获取总行数
			$allRow=$currentSheet->getHighestRow();
			//循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
			for($currentRow=1;$currentRow<=$allRow;$currentRow++){
				//从哪列开始，A表示第一列
				for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
					//数据坐标
					$address=$currentColumn.$currentRow;
					//读取到的数据，保存到数组$arr中
					$cell =$currentSheet->getCell($address)->getValue();
	
					if($cell instanceof PHPExcel_RichText){
						$cell  = $cell->__toString();
					}
					
					$data[$currentColumn]=$cell;
				}
				
				if($currentRow>1)
				{
					$data_sum[$currentRow]=$data;
				}
				
	            
			}  
			$i=0;
			$str='';
			if(!empty($data_sum))
			{  
				foreach($data_sum as $key=>$v)
				{
					if(!empty($v))
					{   
						$data['for_short']	=(string)$v['A'];
					    $data['full_name']	=(string)$v['B'];/*
						$data['principal']	=$v['C'];
						$data['contact']	=$v['D'];
						$data['address']	=$v['E'];
						$data['account']	=$v['F'];
						$data['buyer']		=$v['G'];*/
						
						if($v['C']==NULL){
							$v['C']	='未设置';
							$data['principal']	=(string)$v['C'];
						}else{
							$data['principal']	=(string)$v['C'];
						}
						if($v['D']==NULL){
							$v['D']	='未设置';
							$data['contact']	=(string)$v['D'];
						}else{
							$data['contact']	=(string)$v['D'];
						}
						
						
						if($v['E']==NULL){
							$v['E']	='未设置';
							$data['address']	=(string)$v['E'];
						}else{
							$data['address']	=(string)$v['E'];
						}
						
						if($v['F']==NULL){
							$v['F']	='未设置';
							$data['account']	=(string)$v['F'];
						}else{
							$data['account']	=(string)$v['F'];
						}
						if($v['G']==NULL){
							$v['G']	='未设置';
							$data['buyer']	=(string)$v['G'];
						}else{
							$data['buyer']	=(string)$v['G'];
						}
						
						
						//查询已有供应商，如果有则修改，没有则删除
						$check = M('supplier')->field('for_short')->where(array('for_short'=>$data['for_short']))->find();
						
						if($check)
						{						
							$res = M('supplier')->where(array('for_short'=>$data['for_short']))->save($data);
							if($res!==false)
							{
								$i++;
							}
							else
							{
								$str.=$v['A']." 导入失败<br>";
							}
							
						}
						else
							{
								
							$res= M('supplier')->add($data);		
							if($res)
							{
								$i++;
							}
							else
							{
								$str.=$v['A']."<br>";
							}
						}
						
					}
				}
				//修改采购员
				$list = M()->query("select for_short,buyer from supplier");
				foreach($list as $v){
					$where = " supplier ='".$v['for_short']."'";
					M("product")->where($where)->save(array('buyer'=>$v['buyer']));
				}
				if(!empty($str))
				{   
				    $str.="其余导入成功";
					$prompt = "<div style='font-size:26px;'>以下供应商导入不成功,请及时复制出,检查后编辑再重新导入！</div><br/>";
					echo $prompt.$str;die;
				}
				else
				{
					$this->success('导入成功');
				}
				
				
			}
			else
			{
				$this->error('没有数据');
			}
			
         }
     
    }
		
	
 	
}
















